package com.zbj.sleuth.consumer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

/**
 * SleuthConsumerController
 *
 * @author weigang
 * @create 2019-08-21
 **/
@RestController
@Slf4j
public class SleuthConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/consumer")
    public String consumer(@RequestParam(value = "hello", required = false, defaultValue = "hello") String hello,
                           HttpServletRequest request) {
        // 官方文档为大写 X-B3-TraceId X-B3-SpanId  https://cloud.spring.io/spring-cloud-static/spring-cloud-sleuth/2.1.2.RELEASE/single/spring-cloud-sleuth.html
        String traceId = request.getHeader("x-b3-traceid");
        String spanId = request.getHeader("x-b3-spanid");
        log.info("consumer start traceId-> {} spanId-> {}", traceId, spanId);
        return restTemplate.getForObject("http://sleuth-provider/hello?world=" + hello, String.class);
    }
}